package com.wqtz.chechai.view.activities;

import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.wqtz.chechai.R;
import com.wqtz.chechai.api.Constans;
import com.wqtz.chechai.base.BaseActivity;
import com.wqtz.chechai.bean.CarResult;
import com.wqtz.chechai.dao.HisCity;
import com.wqtz.chechai.dao.hiscity.DaoMaster;
import com.wqtz.chechai.dao.hiscity.DaoSession;
import com.wqtz.chechai.dao.hiscity.HisCityDao;
import com.wqtz.chechai.presenter.BasePresent;
import com.wqtz.chechai.utils.JsonUtils;
import com.wqtz.chechai.utils.NetworkUtils;
import com.wqtz.chechai.utils.StringUtils;
import com.wqtz.chechai.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;


import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.dao.query.QueryBuilder;

/**
 * 项目名称： CheChai
 * 创建人 : Liberation
 * 创建时间: 2016/12/22 17:01
 */

public class NewAddStepTwoActivity extends BaseActivity implements NetworkUtils.GetJson {
	@BindView(R.id.iv_back)
	ImageView mIvBack;
	@BindView(R.id.tv_title)
	TextView mTvTitle;
	@BindView(R.id.iv_vin)
	ImageView mIvVin;
	@BindView(R.id.iv_pinggu)
	ImageView mIvPinggu;
	@BindView(R.id.iv_driving_info)
	ImageView mIvDrivingInfo;
	@BindView(R.id.tv_vin)
	TextView mTvVin;
	@BindView(R.id.tv_edpg)
	TextView mTvEdpg;
	@BindView(R.id.tv_carinfo)
	TextView mTvCarinfo;
	@BindView(R.id.tv_es)
	TextView mTvEs;
	@BindView(R.id.tv_select_buy_car_state)
	TextView mTvSelectBuyCarState;
	@BindView(R.id.tv_car_state)
	TextView mTvCarState;
	@BindView(R.id.ll_select_buy_car_state)
	LinearLayout mLlSelectBuyCarState;
	@BindView(R.id.tv_select_car_model)
	TextView mTvSelectCarModel;
	@BindView(R.id.tv_car_model)
	TextView mTvCarModel;
	@BindView(R.id.ll_select_car_model)
	LinearLayout mLlSelectCarModel;
	@BindView(R.id.tv_select_buy_time)
	TextView mTvSelectBuyTime;
	@BindView(R.id.tv_buy_time)
	TextView mTvBuyTime;
	@BindView(R.id.ll_select_buy_time)
	LinearLayout mLlSelectBuyTime;
	@BindView(R.id.tv_car_run_km)
	TextView mTvCarRunKm;
	@BindView(R.id.et_km)
	EditText mEtKm;
	@BindView(R.id.tv_run_km)
	TextView mTvRunKm;
	@BindView(R.id.ll_car_run_km)
	LinearLayout mLlCarRunKm;
	@BindView(R.id.btn_pingu)
	Button mBtnPingu;
	@BindView(R.id.ll_select_carstatus)
	LinearLayout mCarStatus;
	DaoMaster.DevOpenHelper devOpenHelper;
	DaoMaster daoMaster;
	DaoSession daoSession;
	HisCityDao hisCityDao;
	List<HisCity> list = new ArrayList<>();
	@BindView(R.id.tv_select_carstatus)
	TextView mTvSelectCarstatus;
	@BindView(R.id.tv_carstatus)
	TextView mTvCarstatus;
	private String cityId;//购买地点城市ID
	private String carModelId; //车辆型号对应的ID
	private String carModel ;//车辆型号
	private String year; //购买年份
	private String month;//购买月份
	private String mileage;//行驶的公里数
	private String price;//购买的价钱
	private String carStatus; //车辆状态
	private String url;
	private String provinceId; //省份ID
	private CarResult mResult; //查询结果Bean
	private List<Double> arrs;

	@Override
	public void init() {
		ButterKnife.bind(this);
		devOpenHelper = new DaoMaster.DevOpenHelper(this, "hisCity", null);
		daoMaster = new DaoMaster(devOpenHelper.getWritableDatabase());
		daoSession = daoMaster.newSession();
		hisCityDao = daoSession.getHisCityDao();

	}

	@Override
	public void loadData() {

	}

	@Override
	public int setContentViews() {
		return R.layout.activity_new_add_steptwo;
	}

	@Override
	public BasePresent createPresenter() {
		return null;
	}


	@OnClick({R.id.iv_back, R.id.tv_title, R.id.ll_select_buy_car_state, R.id.ll_select_car_model, R.id.ll_select_buy_time, R.id.ll_car_run_km, R.id.btn_pingu, R.id.ll_select_carstatus})
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.iv_back:
				this.finish();
				break;
			case R.id.tv_title:
				break;
			case R.id.ll_select_buy_car_state:
				Intent intent = new Intent(this, NewAddStepTwoCarState1.class);
				startActivityForResult(intent, 300);
				break;
			case R.id.ll_select_car_model:
				Intent intent1 = new Intent(this, CarBrandSelectActivity.class);
				startActivityForResult(intent1, 1003);
				break;
			case R.id.ll_select_buy_time:
				Intent intent2 = new Intent(this, TimeSelectActivity.class);
				startActivityForResult(intent2, 1006);
				break;
			case R.id.ll_select_carstatus: {
				Intent intent3 = new Intent(this, CarStatusActivity.class);
				startActivityForResult(intent3, 1008);
			}
			break;
			case R.id.ll_car_run_km:
				break;
			case R.id.btn_pingu:
				if(!StringUtils.isEmpty(mEtKm.getText().toString())){
					mileage =  mEtKm.getText().toString();
					url = String.format(Constans.CAR_ASSUM_PRICE,carStatus,1,provinceId,cityId,carModelId,year,month,mileage,price);
					NetworkUtils.doHttpAsync(url,this,this);
				}
				break;
		}
	}


	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 300 && resultCode == RESULT_OK) {
			cityId = data.getStringExtra("city_id");
			provinceId = data.getStringExtra("pro_id");
			mTvCarState.setText(data.getStringExtra("city_name"));
			HisCity hisCity = new HisCity(null, data.getStringExtra("city_id"), data.getStringExtra("city_name"), data.getStringExtra("pro_id") + "", "", "", "", "", "", 0);
			if (gethisCity(hisCity).size() == 0) {
				hisCityDao.insert(hisCity);
			}
		}

		if (requestCode == 1003 && resultCode == RESULT_OK) {

			if (data.getStringExtra("cxname") != null) {
				carModel = data.getStringExtra("cxname");
				if (carModel.toCharArray().length > 8) {
					mTvCarModel.setText((carModel.substring(0, 8)) + "...");
				} else {
					mTvCarModel.setText(carModel);
				}
			}
			price = data.getStringExtra("price");
			carModelId = data.getStringExtra("id");


		}
		if (requestCode == 1006 && resultCode == RESULT_OK) {
			year = data.getStringExtra("year");
			month = data.getStringExtra("month")+"";
			mTvBuyTime.setText(year + "年" + month + "月");
		}

		if (requestCode == 1008 && resultCode == RESULT_OK) {
			carStatus = data.getStringExtra("level");
			mTvCarstatus.setText(data.getStringExtra("status"));

		}
	}

	private List<HisCity> gethisCity(HisCity hisCity) {
		list.clear();
		QueryBuilder<HisCity> qb = hisCityDao.queryBuilder();
		qb.where(HisCityDao.Properties.CityID.eq(hisCity.getCityID())).orderAsc(HisCityDao.Properties.ProID);
		return list = qb.list();
	}


	@Override
	public void getJsonCallBack(String result, String request_url) {
		if(result!=null&&request_url.equals(url)){
			Logger.e(result);
			mResult = new CarResult();
			mResult=JsonUtils.getCarResult(result);
			if (mResult.getError_code()==0){
				Logger.e("判断成功，跳转");
				Intent intent = new Intent(NewAddStepTwoActivity.this,ResultActivity.class);
				intent.putExtra("cityId",cityId);
				intent.putExtra("provinceId",provinceId);
				intent.putExtra("carModelId",carModelId);
				intent.putExtra("carModel",carModel);
				intent.putExtra("buyTime","购车时间:"+year+"年"+month+"月");
				intent.putExtra("mileage",mileage);
				intent.putExtra("carStatus",carStatus);
				arrs = new ArrayList<>();
				arrs.addAll(mResult.getResult().getEst_price_result()) ;
				intent.putExtra("price1",arrs.get(0).toString());
				intent.putExtra("price2",arrs.get(1).toString());
				intent.putExtra("price3",arrs.get(2).toString());
				NewAddStepTwoActivity.this.startActivity(intent);
				this.finish();
			}else {
				ToastUtils.show(this,"输入信息有误！",2000);
			}

		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		activitys.add(this); //添加当前的activity
	}
}
